/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: DetailActivity.java
 * 		Description	: Detail Screen
 * 		Called		: MainActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application;

import java.util.HashMap;
import com.kompas.bola.application.R;
import android.support.v4.app.FragmentActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.adapter.SwipeNavigationAdapter;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.db.CrudHelper;
import com.kompas.bola.application.ui.FontUtil;
import com.kompas.bola.application.utils.Constants;
import com.kompas.bola.application.utils.TimeUtils;
/** =============================================================================== */

public class DetailActivity extends FragmentActivity{
	private TextView timeStamp;
	private RelativeLayout headerViewLayout;
	private SwipeNavigationAdapter page;
	private ViewPager viewPage;	
	private int position;
	private int count_related;
	private String kanal;
	private String guid;
	private HashMap<String, String> value;
	private int countMain;
	private int param;
	/** =========================================================================== */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_screen);  
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);


		viewPage 								= (ViewPager) findViewById(R.id.detail_pager);
		
		Intent in 								= getIntent(); 
		
        guid									= in.getStringExtra(Constants.GUID);
        position								= in.getIntExtra(Constants.POSITION,0);
        kanal									= in.getStringExtra(Constants.KANAL);
        param									= in.getIntExtra(Constants.DETAIL_PARAM, param);
        count_related							= in.getIntExtra(Constants.COUNT_RELATED, count_related); 

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));		
		/*addTimeStamp();*/
		
		/** 
		 * START GOOGLE ANALYTICS */
		value													= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.DETAIL_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);

		CrudHelper db							= new CrudHelper(DetailActivity.this);	

		try {
			switch (param) {
			case Constants.DETAIL_MAIN:
				countMain 						= db.getCountList(kanal != null ? kanal : Constants.EMPTY);				
				break;
			case Constants.DETAIL_BOOKMARK:
				countMain 						= db.getCountBookmark();				
				break;
			default:
				break;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("IS KANAL");
		}
        
        page									= new SwipeNavigationAdapter(getSupportFragmentManager(),
        										kanal,
        										guid,
        										count_related,
        										countMain,
        										param);       
        
		/*renderSwipeView();*/
        this.runOnUiThread(new Runnable(){
			public void run() {    
				viewPage.setAdapter(page);        
		        viewPage.setCurrentItem(position-1);
			}
		});
	}
	/** =========================================================================== */
	private void addTimeStamp(){
		this.timeStamp			= new TextView(this);
		RelativeLayout.LayoutParams timeView = new RelativeLayout.LayoutParams(-1,-2);
		timeView.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		this.timeStamp.setGravity(RelativeLayout.CENTER_VERTICAL);
		this.timeStamp.setBackgroundColor(Color.argb(128, 0, 0, 0));
		this.timeStamp.setVisibility(View.VISIBLE);
		this.timeStamp.setLayoutParams(timeView);
		this.timeStamp.setText(TimeUtils.showLongDateString());
		this.timeStamp.setTypeface(FontUtil.getLucidaBold(this));
		this.timeStamp.setPadding(50, 5, 0, 5);
		this.timeStamp.setTextColor(-1);

	    this.headerViewLayout 	= ((RelativeLayout)findViewById(R.id.container));
	    if (this.headerViewLayout != null)
	      this.headerViewLayout.addView(this.timeStamp);
	}
	/** =========================================================================== */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
			return true;
		default:
			return super.onOptionsItemSelected(item);		
		}		
	}
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	Intent main						= new Intent(DetailActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }	
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();		

		EasyTracker.getInstance(this).activityStart(this);  // Add this method.	
	    FlurryUtil.startSessionWithFlurry(this, Constants.DETAIL_SCREEN);
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();

	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.DETAIL_SCREEN);
	}
	/** =========================== e : Google Analytics ========================== */
}
